---
title: 'Replication Code for Consumer Voting: Affective Judgments in an Uncertain
  Environment'
author: ''
date: "06/16/21"
output:
  html_document:
    toc: yes
    df_print: paged
  latex_engine: xelatex
  number_sections: yes
  pdf_document:
    toc: yes
mainfont: Helvetica
editor_options:
  chunk_output_type: inline
---

# Directions

Below is the exact code used for all data cleaning and analysis. To replicate the findings in this study (including additional analysis in the appendix), please use the below code with the provided data.

# Setting up the Data {.tabset}

##  Loading packages and setting working directory (hidden to maintain anonymity)

```{r setup, echo = FALSE, message = FALSE, results = 'hide'}

library(mfx)
library(arm)
library(boot)
library(Zelig)
library(stargazer)
library(descr)
library(ggplot2)
library(ggpubr)
library(psych)
library(GPArotation)
library(foreign)
library(apsrtable)
library(frm)
library(sjPlot)
library(Rmisc)

setwd("~/Dropbox/Research/Uncertainty/RAP") # insert your working directory here
knitr::opts_knit$set(root.dir = '~/Dropbox/Research/Uncertainty/RAP') # insert your working directory here

```

## Loading data

```{r}

dat <- read.csv("data.csv")

```

## Cleaning the Data

```{r}

# Uncertain Affect
dat$un.aff <- NA
dat$un.aff <- ifelse(dat$Q457 == 1, 1, 0)
dat[is.na(dat$un.aff) == T,]$un.aff <- 0

# Uncertain Cognitive
dat$un.cog <- NA
dat$un.cog <- ifelse(dat$Q473 == 1, 1, 0)
dat[is.na(dat$un.cog) == T,]$un.cog <- 0

# Uncertain Non-Affect
dat$un.non.aff <- NA
dat$un.non.aff <- ifelse(dat$Q465 == 1, 1, 0)
dat[is.na(dat$un.non.aff) == T,]$un.non.aff <- 0

# Uncertain Non-Cognitive
dat$un.non.cog <- NA
dat$un.non.cog <- ifelse(dat$Q481 == 1, 1, 0)
dat[is.na(dat$un.non.cog) == T,]$un.non.cog <- 0

# Certain Affect
dat$cert.aff <- NA
dat$cert.aff <- ifelse(dat$Q489 == 1, 1, 0)
dat[is.na(dat$cert.aff) == T,]$cert.aff <- 0

# Certain Cognitive
dat$cert.cog <- NA
dat$cert.cog <- ifelse(dat$Q497 == 1, 1, 0)
dat[is.na(dat$cert.cog) == T,]$cert.cog <- 0

# Certain Non-Affect
dat$cert.non.aff <- NA
dat$cert.non.aff <- ifelse(dat$Q505 == 1, 1, 0)
dat[is.na(dat$cert.non.aff) == T,]$cert.non.aff <- 0

# Certain Non-Cognitive
dat$cert.non.cog <- NA
dat$cert.non.cog <- ifelse(dat$Q513 == 1, 1, 0)
dat[is.na(dat$cert.non.cog) == T,]$cert.non.cog <- 0

# Treatment Group Variable
## 1 = un.aff, 2 = un.cog, 3 = un.non.aff, 4 = un.non.cog
## 5 = cert.aff, 6 = cert.cog, 7 = cert.non.aff, 8 = cert.non.cog

dat$treat.gr <- NA
dat$treat.gr <- ifelse(dat$un.aff == 1, 1,
                       ifelse(dat$un.cog == 1, 2,
                              ifelse(dat$un.non.aff == 1, 3,
                                     ifelse(dat$un.non.cog == 1, 4,
                                            ifelse(dat$cert.aff == 1, 5,
                                                   ifelse(dat$cert.cog == 1, 6,
                                                          ifelse(dat$cert.non.aff == 1, 7,
                                                                 ifelse(dat$cert.non.cog == 1, 8, NA))))))))

dat$un.gr <- NA
dat$un.gr <- ifelse(dat$treat.gr < 5, 1, 0)

dat$aff.gr.1 <- NA
dat$aff.gr.1 <- ifelse((dat$treat.gr == 1 | dat$treat.gr == 3) |
                        (dat$treat.gr == 5 | dat$treat.gr == 7), 1, 0)

dat$aff.gr <- NA
dat$aff.gr <- ifelse((dat$treat.gr == 1 | dat$treat.gr == 5), 1, 
                     ifelse((dat$treat.gr == 3 | dat$treat.gr == 7), 0, NA))

## Voting Variables

dat$v1 <- NA
dat$v1 <- dat$Q462_1
dat[is.na(dat$v1), ]$v1 <- 0

dat$v2 <- NA
dat$v2 <- dat$Q478_1
dat[is.na(dat$v2), ]$v2 <- 0

dat$v3 <- NA
dat$v3 <- dat$Q470_1
dat[is.na(dat$v3), ]$v3 <- 0

dat$v4 <- NA
dat$v4 <- dat$Q486_1
dat[is.na(dat$v4), ]$v4 <- 0

dat$v5 <- NA
dat$v5 <- dat$Q494_1
dat[is.na(dat$v5), ]$v5 <- 0

dat$v6 <- NA
dat$v6 <- dat$Q502_1
dat[is.na(dat$v6), ]$v6 <- 0

dat$v7 <- NA
dat$v7 <- dat$Q510_1
dat[is.na(dat$v7), ]$v7 <- 0

dat$v8 <- NA
dat$v8 <- dat$Q518_1
dat[is.na(dat$v8), ]$v8 <- 0

dat$voting <- NA
dat$voting <- dat$v1 + dat$v2 + dat$v3 + dat$v4 +
  dat$v5 + dat$v6 + dat$v7 + dat$v8

### Certainty Variables

dat$c1 <- NA
dat$c1 <- dat$Q463_1
dat[is.na(dat$c1), ]$c1 <- 0

dat$c2 <- NA
dat$c2 <- dat$Q479_1
dat[is.na(dat$c2), ]$c2 <- 0

dat$c3 <- NA
dat$c3 <- dat$Q471_1
dat[is.na(dat$c3), ]$c3 <- 0

dat$c4 <- NA
dat$c4 <- dat$Q487_1
dat[is.na(dat$c4), ]$c4 <- 0

dat$c5 <- NA
dat$c5 <- dat$Q495_1
dat[is.na(dat$c5), ]$c5 <- 0

dat$c6 <- NA
dat$c6 <- dat$Q503_1
dat[is.na(dat$c6), ]$c6 <- 0

dat$c7 <- NA
dat$c7 <- dat$Q511_1
dat[is.na(dat$c7), ]$c7 <- 0

dat$c8 <- NA
dat$c8 <- dat$Q519_1
dat[is.na(dat$c8), ]$c8 <- 0

dat$certainty <- NA
dat$certainty <- dat$c1 + dat$c2 + dat$c3 + dat$c4 +
  dat$c5 + dat$c6 + dat$c7 + dat$c8

### Pleasant Variables

dat$p1 <- NA
dat$p1 <- dat$Q464_1
dat[is.na(dat$p1), ]$p1 <- 0

dat$p2 <- NA
dat$p2 <- dat$Q480_1
dat[is.na(dat$p2), ]$p2 <- 0

dat$p3 <- NA
dat$p3 <- dat$Q472_1
dat[is.na(dat$p3), ]$p3 <- 0

dat$p4 <- NA
dat$p4 <- dat$Q488_1
dat[is.na(dat$p4), ]$p4 <- 0

dat$p5 <- NA
dat$p5 <- dat$Q496_1
dat[is.na(dat$p5), ]$p5 <- 0

dat$p6 <- NA
dat$p6 <- dat$Q504_1
dat[is.na(dat$p6), ]$p6 <- 0

dat$p7 <- NA
dat$p7 <- dat$Q512_1
dat[is.na(dat$p7), ]$p7 <- 0

dat$p8 <- NA
dat$p8 <- dat$Q520_1
dat[is.na(dat$p8), ]$p8 <- 0

dat$pleasant <- NA
dat$pleasant <- dat$p1 + dat$p2 + dat$p3 + dat$p4 +
  dat$p5 + dat$p6 + dat$p7 + dat$p8

#### Duration limiting variable #####

dat$dur <- ifelse(dat$Duration > 600, 1, 0)

#### PID #####

dat$pid1 <- as.numeric(dat$Q365)
dat$pid2 <- as.numeric(dat$Q367)
dat$pid3 <- as.numeric(dat$Q369)
dat$pid4 <- as.numeric(dat$Q371)

dat$pid <- NA
dat$pid <- ifelse(dat$Q369 == 'Strong Democrat', 0,
           ifelse(dat$Q369 == 'Not very strong Democrat', 1,
           ifelse(dat$Q371 == 'Closer to the Democrats', 2,
           ifelse(dat$Q371 == 'Neither', 3,
           ifelse(dat$Q371 == 'Closer to the Republicans', 4,
           ifelse(dat$Q367 == 'Not very strong Republican', 5,
           ifelse(dat$Q367 == 'Strong Republican', 6, NA)))))))

dat$pidstr <- NA
dat$pidstr <- ifelse(dat$pid == 0 | dat$pid == 6, 3,
              ifelse(dat$pid == 1 | dat$pid == 5, 2,
              ifelse(dat$pid == 2 | dat$pid == 4, 1, 0)))
dat$pidstr <- dat$pidstr / 3

dat$edu <- ifelse(dat$Q363 == 'High school or less', 0,
           ifelse(dat$Q363 == 'Some college/Vocational training', 1,
           ifelse(dat$Q363 == 'College graduate', 2,
           ifelse(dat$Q363 == 'Some post-graduate training', 3,
           ifelse(dat$Q363 == 'Post-graduate degree', 4, NA)))))

dat$age <- ifelse(dat$Q361 == '18 - 24', 0,
           ifelse(dat$Q361 == '25 - 34', 1,
           ifelse(dat$Q361 == '35 - 44', 2,
           ifelse(dat$Q361 == '45 - 54', 3,
           ifelse(dat$Q361 == '55 - 64', 4,
           ifelse(dat$Q361 == '65 - 74', 5,
           ifelse(dat$Q361 == '75 - 84', 6,
           ifelse(dat$Q361 == '85 or older', 7, NA))))))))

# dat$race <- as.numeric(dat$Q359)

dat$gender <- ifelse(dat$Q357 == 'Female', 1,
              ifelse(dat$Q357 == 'Male', 0, NA))

```

# Modeling Experiment Effects {.tabset}

```{r}

#### Plots #####

dat1 <- dat[dat$dur == 1, ]
dat.rep <- dat1[dat1$pid > 3, ]
dat.dem <- dat1[dat1$pid < 3, ]
pre.df <- group.CI(voting ~ treat.gr,
                   data = dat1,
                   ci = 0.90)
rep.df <- group.CI(voting ~ treat.gr,
                   data = dat.rep,
                   ci = 0.90)
dem.df <- group.CI(voting ~ treat.gr,
                   data = dat.dem,
                   ci = 0.90)

gr.name <- c("Affect", "Cognitive",
             "Affect", "Cognitive",
             "Affect", "Cognitive", 
             "Affect", "Cognitive")

lev.group <- c("Positive/High", "Positive/High",
               "Negative/Low", "Negative/Low",
               "Positive/High", "Positive/High",
               "Negative/Low", "Negative/Low")

tr.group <- c(rep("Uncertainty Prime", times = 4),
              rep("Certainty Prime", times = 4))
pre.df <- cbind(pre.df, gr.name)
pre.df <- cbind(pre.df, tr.group)
pre.df <- cbind(pre.df, lev.group)

rep.df <- cbind(rep.df, gr.name)
rep.df <- cbind(rep.df, tr.group)
rep.df <- cbind(rep.df, lev.group)

dem.df <- cbind(dem.df, gr.name)
dem.df <- cbind(dem.df, tr.group)
dem.df <- cbind(dem.df, lev.group)


cert.df <- group.CI(certainty ~ treat.gr,
                    data = dat,
                    ci = 0.90)

cert.df <- cbind(cert.df, gr.name)
cert.df <- cbind(cert.df, tr.group)
cert.df <- cbind(cert.df, lev.group)

pleas.df <- group.CI(pleasant ~ treat.gr,
                     data = dat,
                     ci = 0.90)

pleas.df <- cbind(pleas.df, gr.name)

#set_theme(theme.font = "Times")

aff.df <- pre.df[pre.df$gr.name == "Affect",]
aff.df$lev.group <- ifelse(aff.df$lev.group == 'Positive/High', 'Positive',
                    'Negative')

aff.plot <- ggplot(data = aff.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nTreatment Group') +
  coord_cartesian(ylim=c(50,80)) +
  ylab('Average Likelihood of Voting') +
  labs(title = "Effect of Prime on Likelihood of Voting--Affective Inputs") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
aff.plot
#ggsave(aff.plot, file = "aff-plot.pdf", height = 4, width = 6)

aff.rep.df <- rep.df[rep.df$gr.name == "Affect",]
aff.rep.df$lev.group <- ifelse(aff.rep.df$lev.group == 'Positive/High', 'Positive',
                           'Negative')

aff.rep.plot <- ggplot(data = aff.rep.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('') +
  ylab('') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Republicans") +
  theme(legend.position="right") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
aff.rep.plot

aff.dem.df <- dem.df[dem.df$gr.name == "Affect",]
aff.dem.df$lev.group <- ifelse(aff.dem.df$lev.group == 'Positive/High', 'Positive',
                               'Negative')
aff.dem.plot <- ggplot(data = aff.dem.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('') +
  ylab('') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Democrats") +
  theme(legend.position = 'right') + 
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
aff.dem.plot

party.aff <- ggarrange(aff.rep.plot, aff.dem.plot, common.legend = T, legend = "right")
annotate_figure(party.aff,
                bottom = text_grob("Treatment Group", family = 'Times'),
                left = text_grob("Average Likelihood of Voting", rot = 90, family = 'Times'))

cog.df <- pre.df[pre.df$gr.name == "Cognitive",]
cog.df$lev.group <- ifelse(cog.df$lev.group == 'Positive/High', 'High',
                               'Low')

cog.plot <- ggplot(data = cog.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nTreatment Group') +
  ylab('Average Likelihood of Voting') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Effect of Prime on Likelihood of Voting--Cognitive Inputs") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  theme(legend.position="right") +
  guides(fill=guide_legend(title="Cue"))
cog.plot
#ggsave(cog.plot, file = "cog-plot.pdf", height = 4, width = 6)

cog.rep.df <- rep.df[rep.df$gr.name == "Cognitive",]
cog.rep.df$lev.group <- ifelse(cog.rep.df$lev.group == 'Positive/High', 'High',
                           'Low')

cog.rep.plot <- ggplot(data = cog.rep.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('') +
  ylab('') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Republicans") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
cog.rep.plot

cog.dem.df <- dem.df[dem.df$gr.name == "Cognitive",]
cog.dem.df$lev.group <- ifelse(cog.dem.df$lev.group == 'Positive/High', 'High',
                               'Low')

cog.dem.plot <- ggplot(data = cog.dem.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('') +
  ylab('') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Democrats") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
cog.dem.plot

party.cog <- ggarrange(cog.rep.plot, cog.dem.plot, common.legend = T, legend = "right")

annotate_figure(party.cog,
                bottom = text_grob("Treatment Group", family = 'Times'),
                left = text_grob("Average Likelihood of Voting", rot = 90, family = 'Times'))

### Affect (Ad) versus Cognitive over all

dat$aff <- ifelse((dat$un.aff == 1 | dat$un.non.aff == 1) |
                   (dat$cert.aff == 1 | dat$cert.non.aff == 1), 1, 0)
dat$uncert <- ifelse((dat$un.aff == 1 | dat$un.cog == 1) |
                     (dat$un.non.aff == 1 | dat$un.non.cog == 1), 1, 0)

dat$gen.gr <- ifelse(dat$aff == 1 & dat$uncert == 1, 1,
              ifelse(dat$aff == 1 & dat$uncert == 0, 2,
              ifelse(dat$aff == 0 & dat$uncert == 1, 3, 4)))

grouped.df <- group.CI(voting ~ gen.gr,
                    data = dat,
                    ci = 0.90)

uncert.gr <- c("Uncertain", "Certain", "Uncertain", "Certain")
aff.gr <- c("Affect", "Affect", "Cognitive", "Cognitive")

grouped.df <- cbind(grouped.df, uncert.gr)
grouped.df <- cbind(grouped.df, aff.gr)


grouped.plot <- ggplot(data = grouped.df, aes(x = uncert.gr, y = voting.mean,
                                      fill = aff.gr)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nTreatment Group') +
  ylab('Average Likelihood of Voting') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Effect of Prime on Likelihood of Voting by Affect vs. Cognitive Treatment") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue Group"))
grouped.plot
#ggsave(grouped.plot, file = "grouped-plot.pdf", height = 4, width = 6)

grouped.df2 <- group.CI(pleasant ~ gen.gr,
                       data = dat,
                       ci = 0.90)

grouped.df2 <- cbind(grouped.df2, uncert.gr)
grouped.df2 <- cbind(grouped.df2, aff.gr)


grouped.plot2 <- ggplot(data = grouped.df2, aes(x = uncert.gr, y = pleasant.mean,
                                              fill = aff.gr)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = pleasant.lower, ymax = pleasant.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nPrime Group') +
  ylab('Average Pleasantness Rating') +
  coord_cartesian(ylim=c(30,80)) +
  labs(title = "Effect of Advertisement Cue on Pleasantness Rating") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue Group"))
grouped.plot2
#ggsave(grouped.plot2, file = "grouped-plot2.pdf", height = 4, width = 6)

#### Manipulation Checks #####

gr.cert.df <- group.CI(certainty ~ gen.gr,
                       data = dat,
                       ci = 0.90)

gr.cert.df <- cbind(gr.cert.df, uncert.gr)
gr.cert.df <- cbind(gr.cert.df, aff.gr)

cert.plot <- ggplot(data = gr.cert.df, aes(x = aff.gr, y = certainty.mean,
                                      fill = uncert.gr)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = certainty.lower, ymax = certainty.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nCue Type') +
  ylab('Average Certainty') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Effect of Prime on Likelihood of Feeling Certain") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Prime Group"))
cert.plot
#ggsave(cert.plot, file = "cert-plot.pdf", height = 4, width = 6)

gr.pl.df <- group.CI(pleasant ~ treat.gr,
                       data = dat,
                       ci = 0.90)

gr.pl.df <- cbind(gr.pl.df, gr.name)
gr.pl.df <- cbind(gr.pl.df, tr.group)
gr.pl.df <- cbind(gr.pl.df, lev.group)
gr.pl.df <- gr.pl.df[gr.pl.df$gr.name == 'Affect',]
gr.pl.df$lev.group <- ifelse(gr.pl.df$lev.group == 'Positive/High', 'Positive',
                               'Negative')

pl.plot <- ggplot(data = gr.pl.df, aes(x = tr.group, y = pleasant.mean,
                                           fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = pleasant.lower, ymax = pleasant.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nPrime Group') +
  ylab('Average Pleasantness Rating') +
  coord_cartesian(ylim = c(50, 80)) +
  labs(title = "Effect of Prime on Pleasant Evaluation of Article") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
pl.plot
#ggsave(pl.plot, file = "pl-plot.pdf", height = 4, width = 6)

```

## Modeling Partisan Differences

```{r}

#### Modeling Party Difference #####

dat$pid2 <- dat$pid / 6
dat$treat.grp.f <- as.factor(dat$treat.gr)

m <- lm(voting ~ treat.grp.f,
        data = dat)
c.m <- lm(voting ~ treat.grp.f,
          data = subset(dat, comp2 == 1))

m1 <- lm(voting ~ treat.grp.f + pid2 + treat.grp.f*pid2,
        data = dat)

m2 <- lm(voting ~ treat.grp.f + pidstr + treat.grp.f*pidstr,
         data = dat)

c.m1 <- lm(voting ~ treat.grp.f + pid2 + treat.grp.f*pid2,
         data = subset(dat, comp2 == 1))

c.m2 <- lm(voting ~ treat.grp.f + pidstr + treat.grp.f*pidstr,
         data = subset(dat, comp2 == 1))

# apsrtable(m, c.m)
# apsrtable(m1, m2)
# apsrtable(c.m1, c.m2)

summary(m); summary(c.m); summary(m1); summary(m2); summary(c.m1); summary(c.m2)

m_1 <- lm(voting ~ factor(un.gr) + factor(aff.gr.1) +
            factor(un.gr)*factor(aff.gr.1),
          data = dat)
summary(m_1)

## Compliance by group figure

comp.rate = rep(NA, 8)

for (i in 1:8) {
  comp.rate[i] = sum(dat$comp2 == 1 & dat$treat.gr == i, na.rm = T)/sum(dat$treat.gr == i, na.rm = T)
}

comp.rate <- as.numeric(round(comp.rate, digits = 3)*100)

comp.df <- as.data.frame(cbind(comp.rate, c('1','2','3','4','5','6','7','8')))
colnames(comp.df) <- c('comp.rate', 'treat.gr')
comp.df$comp.rate <- as.numeric(comp.df$comp.rate)

comp.gr.plot <- ggplot(comp.df, aes(x = treat.gr, y = comp.rate)) +
  geom_bar(stat = 'identity') +
  ylim(0, 100) +
  theme(text=element_text(family="serif")) +
  ylab('Compliance Rate (Percentage)') +
  xlab('Treatment Group') + 
 # ggtitle('Compliance Rate Across Treatment Groups, mean compliance = 48.5%') +
  theme(plot.title = element_text(hjust = 0.5)) + theme(text=element_text(family="serif"))
#ggsave(comp.gr.plot, file = "compgroupplot.pdf", height = 4, width = 6)

## Checking for compliance bias

comp.m1 <- lm(comp2 ~ un.gr + aff.gr.1 + pid + gender + edu + age,
              data = dat)
summary(comp.m1)
nobs(comp.m1)

#apsrtable(comp.m1)

```

# Compliance Check {.tabset}

```{r}

#### Models #####

dat2 <- subset(dat, comp2 == 1)
dat.rep <- dat[dat2$pid > 3, ]
dat.dem <- dat[dat2$pid < 3, ]
pre.df2 <- group.CI(voting ~ treat.gr,
                   data = dat2,
                   ci = 0.90)
rep.df <- group.CI(voting ~ treat.gr,
                   data = dat.rep,
                   ci = 0.90)
dem.df <- group.CI(voting ~ treat.gr,
                   data = dat.dem,
                   ci = 0.90)

gr.name <- c("Affect", "Cognitive",
             "Affect", "Cognitive",
             "Affect", "Cognitive", 
             "Affect", "Cognitive")

lev.group <- c("Positive/High", "Positive/High",
               "Negative/Low", "Negative/Low",
               "Positive/High", "Positive/High",
               "Negative/Low", "Negative/Low")

tr.group <- c(rep("Uncertainty Prime", times = 4),
              rep("Certainty Prime", times = 4))
pre.df2 <- cbind(pre.df2, gr.name)
pre.df2 <- cbind(pre.df2, tr.group)
pre.df2 <- cbind(pre.df2, lev.group)

rep.df <- cbind(rep.df, gr.name)
rep.df <- cbind(rep.df, tr.group)
rep.df <- cbind(rep.df, lev.group)

dem.df <- cbind(dem.df, gr.name)
dem.df <- cbind(dem.df, tr.group)
dem.df <- cbind(dem.df, lev.group)


cert.df2 <- group.CI(certainty ~ treat.gr,
                    data = dat2,
                    ci = 0.90)

cert.df2 <- cbind(cert.df2, gr.name)
cert.df2 <- cbind(cert.df2, tr.group)
cert.df2 <- cbind(cert.df2, lev.group)

pleas.df2 <- group.CI(pleasant ~ treat.gr,
                     data = dat2,
                     ci = 0.90)

pleas.df2 <- cbind(pleas.df2, gr.name)

#set_theme(theme.font = "Times")

aff.df2 <- pre.df2[pre.df2$gr.name == "Affect",]
aff.df2$lev.group <- ifelse(aff.df2$lev.group == 'Positive/High', 'Positive',
                           'Negative')

aff.plot2 <- ggplot(data = aff.df2, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nTreatment Group') +
  coord_cartesian(ylim=c(50,80)) +
  ylab('Average Likelihood of Voting') +
  labs(title = "Effect of Prime on Likelihood of Voting--Affective Inputs") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
aff.plot2
#ggsave(aff.plot2, file = "aff-plot2.pdf", height = 4, width = 6)

cog.df2 <- pre.df2[pre.df2$gr.name == "Cognitive",]
cog.df2$lev.group <- ifelse(cog.df2$lev.group == 'Positive/High', 'High',
                           'Low')

cog.plot2 <- ggplot(data = cog.df2, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nTreatment Group') +
  ylab('Average Likelihood of Voting') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Effect of Prime on Likelihood of Voting--Cognitive Inputs") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  theme(legend.position="right") +
  guides(fill=guide_legend(title="Cue"))
cog.plot2
#ggsave(cog.plot2, file = "cog-plot2.pdf", height = 4, width = 6)


####### Party Compliance ####################################################

aff.rep.df <- rep.df[rep.df$gr.name == "Affect",]
aff.rep.df$lev.group <- ifelse(aff.rep.df$lev.group == 'Positive/High', 'Positive',
                           'Negative')

aff.rep.plot <- ggplot(data = aff.rep.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('') +
  ylab('') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Republicans") +
  theme(legend.position="right") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
aff.rep.plot

aff.dem.df <- dem.df[dem.df$gr.name == "Affect",]
aff.dem.df$lev.group <- ifelse(aff.dem.df$lev.group == 'Positive/High', 'Positive',
                               'Negative')
aff.dem.plot <- ggplot(data = aff.dem.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('') +
  ylab('') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Democrats") +
  theme(legend.position = 'right') + 
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
aff.dem.plot

party.affc <- ggarrange(aff.rep.plot, aff.dem.plot, common.legend = T, legend = "right")
annotate_figure(party.affc,
                bottom = text_grob("Treatment Group", family = 'Times'),
                left = text_grob("Average Likelihood of Voting", rot = 90, family = 'Times'))

####### Cognitive Party Compliance ####################################################

cog.rep.df <- rep.df[rep.df$gr.name == "Cognitive",]
cog.rep.df$lev.group <- ifelse(cog.rep.df$lev.group == 'Positive/High', 'High',
                           'Low')

cog.rep.plot <- ggplot(data = cog.rep.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('') +
  ylab('') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Republicans") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
cog.rep.plot

cog.dem.df <- dem.df[dem.df$gr.name == "Cognitive",]
cog.dem.df$lev.group <- ifelse(cog.dem.df$lev.group == 'Positive/High', 'High',
                               'Low')

cog.dem.plot <- ggplot(data = cog.dem.df, aes(x = tr.group, y = voting.mean,
                                      fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('') +
  ylab('') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Democrats") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
cog.dem.plot

party.cog <- ggarrange(cog.rep.plot, cog.dem.plot, common.legend = T, legend = "right")

annotate_figure(party.cog,
                bottom = text_grob("Treatment Group", family = 'Times'),
                left = text_grob("Average Likelihood of Voting", rot = 90, family = 'Times'))

```

# Manipulation Checks {.tabset}

```{r}

## Manipulation Checks

dat2$aff <- ifelse((dat2$un.aff == 1 | dat2$un.non.aff == 1) |
                    (dat2$cert.aff == 1 | dat2$cert.non.aff == 1), 1, 0)
dat2$uncert <- ifelse((dat2$un.aff == 1 | dat2$un.cog == 1) |
                       (dat2$un.non.aff == 1 | dat2$un.non.cog == 1), 1, 0)

dat2$gen.gr <- ifelse(dat2$aff == 1 & dat2$uncert == 1, 1,
                     ifelse(dat2$aff == 1 & dat2$uncert == 0, 2,
                            ifelse(dat2$aff == 0 & dat2$uncert == 1, 3, 4)))

grouped.df3 <- group.CI(voting ~ gen.gr,
                       data = dat2,
                       ci = 0.90)

grouped.df3 <- cbind(grouped.df3, uncert.gr)
grouped.df3 <- cbind(grouped.df3, aff.gr)


grouped.plot3 <- ggplot(data = grouped.df3, aes(x = uncert.gr, y = voting.mean,
                                              fill = aff.gr)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = voting.lower, ymax = voting.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nTreatment Group') +
  ylab('Average Likelihood of Voting') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Effect of Prime on Likelihood of Voting by Affect vs. Cognitive Treatment") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue Group"))
grouped.plot3
#ggsave(grouped.plot3, file = "grouped-plot3.pdf", height = 4, width = 6)

grouped.df4 <- group.CI(pleasant ~ gen.gr,
                        data = dat2,
                        ci = 0.90)

grouped.df4 <- cbind(grouped.df4, uncert.gr)
grouped.df4 <- cbind(grouped.df4, aff.gr)


grouped.plot4 <- ggplot(data = grouped.df4, aes(x = uncert.gr, y = pleasant.mean,
                                                fill = aff.gr)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = pleasant.lower, ymax = pleasant.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nPrime Group') +
  ylab('Average Pleasantness Rating') +
  coord_cartesian(ylim=c(30,80)) +
  labs(title = "Effect of Advertisement Cue on Pleasantness Rating") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue Group"))
grouped.plot4
#ggsave(grouped.plot4, file = "grouped-plot4.pdf", height = 4, width = 6)

#### Manipulation Checks 2 #####

gr.cert.df2 <- group.CI(certainty ~ gen.gr,
                       data = dat2,
                       ci = 0.90)

gr.cert.df2 <- cbind(gr.cert.df2, uncert.gr)
gr.cert.df2 <- cbind(gr.cert.df2, aff.gr)

cert.plot2 <- ggplot(data = gr.cert.df2, aes(x = aff.gr, y = certainty.mean,
                                           fill = uncert.gr)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = certainty.lower, ymax = certainty.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nCue Type') +
  ylab('Average Certainty') +
  coord_cartesian(ylim=c(50,80)) +
  labs(title = "Effect of Prime on Likelihood of Feeling Certain") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Prime Group"))
cert.plot2
#ggsave(cert.plot2, file = "cert-plot2.pdf", height = 4, width = 6)

gr.pl.df2 <- group.CI(pleasant ~ treat.gr,
                     data = dat2,
                     ci = 0.90)

gr.pl.df2 <- cbind(gr.pl.df2, gr.name)
gr.pl.df2 <- cbind(gr.pl.df2, tr.group)
gr.pl.df2 <- cbind(gr.pl.df2, lev.group)
gr.pl.df2 <- gr.pl.df2[gr.pl.df2$gr.name == 'Affect',]
gr.pl.df2$lev.group <- ifelse(gr.pl.df2$lev.group == 'Positive/High', 'Positive',
                             'Negative')

pl.plot2 <- ggplot(data = gr.pl.df2, aes(x = tr.group, y = pleasant.mean,
                                       fill = lev.group)) +
  theme_bw() +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_grey() + 
  #geom_line(aes(group = 1)) +
  geom_errorbar(aes(ymin = pleasant.lower, ymax = pleasant.upper),
                size = 0.5, width = 0.1,
                position = position_dodge(0.9)) + 
  xlab('\nPrime Group') +
  ylab('Average Pleasantness Rating') +
  coord_cartesian(ylim = c(50, 85)) +
  labs(title = "Effect of Prime on Pleasant Evaluation of Article") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position="right") +
  theme(text=element_text(family="serif")) +
  guides(fill=guide_legend(title="Cue"))
pl.plot2
#ggsave(pl.plot2, file = "pl-plot2.pdf", height = 4, width = 6)



m1 <- lm(voting ~ factor(treat.gr)*comp2,
         data = dat)
summary(m1)

```

# Harden, Sohkey, Runge Time Compliance Check {.tabset}

```{r}

##### Complier time #######

range(dat$comptime, na.rm = T)
quantile(dat$comptime, na.rm = T)
quantile(dat$comptime[dat$aff.gr == 1], na.rm = T)
quantile(dat$comptime[dat$aff.gr == 0], na.rm = T)

dat$aff.comp.gr <- NA
dat$aff.comp.gr <- ifelse((dat$treat.gr == 1 | dat$treat.gr == 3) | # 1 = affect gr, 0 = cog gr
                      (dat$treat.gr == 5 | dat$treat.gr == 7), 1,
                      ifelse((dat$treat.gr == 2 | dat$treat.gr == 4) |
                            (dat$treat.gr == 6 | dat$treat.gr == 8), 0, NA))

dat$comp.gr <- ifelse(dat$comptime <= 57 & dat$aff.comp.gr == 1, 1, # 1 and 3 are non compliers, 2 and 4 are compliers
               ifelse(dat$comptime > 57 & dat$aff.comp.gr == 1, 2,
               ifelse(dat$comptime <= 64 & dat$aff.comp.gr == 0, 3,
               ifelse(dat$comptime > 64 & dat$aff.comp.gr == 0, 4, NA))))

dat$comp <- ifelse(dat$comp.gr == 1 | dat$comp.gr == 3, 0,
            ifelse(dat$comp.gr == 2 | dat$comp.gr == 4, 1, NA)) # 1 = compliers, 0 = noncompliers

tot.ca <- (((mean(dat$voting[dat$treat.gr == 1], na.rm = T) - mean(dat$voting[dat$treat.gr == 3], na.rm = T)) - 
           ((mean(dat$voting[dat$treat.gr == 2], na.rm = T) - mean(dat$voting[dat$treat.gr == 4], na.rm = T)) -
           ((mean(dat$voting[dat$treat.gr == 5], na.rm = T) - mean(dat$voting[dat$treat.gr == 7], na.rm = T)) -
           ((mean(dat$voting[dat$treat.gr == 6], na.rm = T) - mean(dat$voting[dat$treat.gr == 8], na.rm = T)))))))

ca.crit <- qt(0.975, df = 1041)
ca.sd <- sd(dat$voting)
ca.me <- ca.crit * (ca.sd/sqrt(1041))
tot.ci <- c(tot.ca - ca.me,
           tot.ca + ca.me)

no.ca <- ((mean(dat$voting[dat$treat.gr == 1 & dat$comp == 0], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 3 & dat$comp == 0], na.rm = T) - 
         (mean(dat$voting[dat$treat.gr == 2 & dat$comp == 0], na.rm = T) -
            mean(dat$voting[dat$treat.gr == 4 & dat$comp == 0], na.rm = T) -
         (mean(dat$voting[dat$treat.gr == 5 & dat$comp == 0], na.rm = T) -
            mean(dat$voting[dat$treat.gr == 7 & dat$comp == 0], na.rm = T) -
         (mean(dat$voting[dat$treat.gr == 6 & dat$comp == 0], na.rm = T) -
            mean(dat$voting[dat$treat.gr == 8 & dat$comp == 0], na.rm = T))))))

no.crit <- qt(0.975, df = 529)
no.sd <- sd(dat$voting)
no.me <- no.crit * (no.sd/sqrt(529))
no.ci <- c(no.ca - no.me,
           no.ca + no.me)

no.ca2 <- ((mean(dat$voting[dat$treat.gr == 1 & dat$comp2 == 0], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 3 & dat$comp2 == 0], na.rm = T) - 
             (mean(dat$voting[dat$treat.gr == 2 & dat$comp2 == 0], na.rm = T) -
                mean(dat$voting[dat$treat.gr == 4 & dat$comp2 == 0], na.rm = T) -
                (mean(dat$voting[dat$treat.gr == 5 & dat$comp2 == 0], na.rm = T) -
                   mean(dat$voting[dat$treat.gr == 7 & dat$comp2 == 0], na.rm = T) -
                   (mean(dat$voting[dat$treat.gr == 6 & dat$comp2 == 0], na.rm = T) -
                      mean(dat$voting[dat$treat.gr == 8 & dat$comp2 == 0], na.rm = T))))))

no.crit2 <- qt(0.975, df = 253)
no.sd2 <- sd(dat$voting)
no.me2 <- no.crit2 * (no.sd2/sqrt(253))
no.ci2 <- c(no.ca2 - no.me2,
           no.ca2 + no.me2)

comp.ca <- ((mean(dat$voting[dat$treat.gr == 1 & dat$comp == 1], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 3 & dat$comp == 1], na.rm = T) - 
            (mean(dat$voting[dat$treat.gr == 2 & dat$comp == 1], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 4 & dat$comp == 1], na.rm = T) -
            (mean(dat$voting[dat$treat.gr == 5 & dat$comp == 1], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 7 & dat$comp == 1], na.rm = T) -
            (mean(dat$voting[dat$treat.gr == 6 & dat$comp == 1], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 8 & dat$comp == 1], na.rm = T))))))

comp.crit <- qt(0.975, df = 491)
comp.sd <- sd(dat$voting)
comp.me <- comp.crit * (comp.sd/sqrt(491))
comp.ci <- c(comp.ca - comp.me,
           comp.ca + comp.me)

comp.ca2 <- ((mean(dat$voting[dat$treat.gr == 1 & dat$comp2 == 1], na.rm = T) -
               mean(dat$voting[dat$treat.gr == 3 & dat$comp2 == 1], na.rm = T) - 
               (mean(dat$voting[dat$treat.gr == 2 & dat$comp2 == 1], na.rm = T) -
                  mean(dat$voting[dat$treat.gr == 4 & dat$comp2 == 1], na.rm = T) -
                  (mean(dat$voting[dat$treat.gr == 5 & dat$comp2 == 1], na.rm = T) -
                     mean(dat$voting[dat$treat.gr == 7 & dat$comp2 == 1], na.rm = T) -
                     (mean(dat$voting[dat$treat.gr == 6 & dat$comp2 == 1], na.rm = T) -
                        mean(dat$voting[dat$treat.gr == 8 & dat$comp2 == 1], na.rm = T))))))

comp.crit2 <- qt(0.975, df = 779) # (788 complkiers, minue 9 for means calculated above)
comp.sd2 <- sd(dat$voting)
comp.me2 <- comp.crit2 * (comp.sd2/sqrt(779))
comp.ci2 <- c(comp.ca2 - comp.me2,
             comp.ca2 + comp.me2)

hicomp.ca <- ((mean(dat$voting[dat$treat.gr == 1 & dat$comptime > 80], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 3 & dat$comptime > 80], na.rm = T) - 
            (mean(dat$voting[dat$treat.gr == 2 & dat$comptime > 93], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 4 & dat$comptime > 93], na.rm = T) -
            (mean(dat$voting[dat$treat.gr == 5 & dat$comptime > 80], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 7 & dat$comptime > 80], na.rm = T) -
            (mean(dat$voting[dat$treat.gr == 6 & dat$comptime > 93], na.rm = T) -
             mean(dat$voting[dat$treat.gr == 8 & dat$comptime > 93], na.rm = T))))))

hicomp.crit <- qt(0.975, df = 257)
hicomp.sd <- sd(dat$voting)
hicomp.me <- hicomp.crit * (hicomp.sd/sqrt(257))
hicomp.ci <- c(hicomp.ca - hicomp.me,
             hicomp.ca + hicomp.me)

ca.pe <- rbind(tot.ca, no.ca, no.ca2, comp.ca, comp.ca2, hicomp.ca)
ca.ci <- rbind(tot.ci, no.ci, no.ci2, comp.ci, comp.ci2, hicomp.ci)
ca.names <- c('ITT', 'CACE: Non-Compliers (Time)', 'CACE: Non-Compliers (Substance)',
              'CACE: Compliers (Time)', 'CACE:Compliers (Substance)',
              'CACE: High Compliers (Time)')

comp.df <- as.data.frame(cbind(ca.pe, ca.ci))
comp.df <- cbind(comp.df, ca.names)
rownames(comp.df) <- c('ITT', 'Non-Compliers (Time)', 'Non-Compliers (Substance)',
                       'Compliers (Time)', 'Compliers (Substance)', 'High Compliers')
colnames(comp.df) <- c('pe', 'lower', 'upper', 'group')

ca.plot <- ggplot(comp.df, aes(x = pe, y = group)) +
           geom_point() +
           geom_errorbarh(aes(xmin = lower, xmax = upper), height = 0.2) +
           geom_vline(xintercept = 0, color = 'red') +
           ylab('Estimand') +
           xlab('Treatment Effect') + 
           theme(plot.title = element_text(hjust = 0.5)) + theme(text=element_text(family="serif"))

## Compliance by group figure

comp.rate = rep(NA, 8)

for (i in 1:8) {
  comp.rate[i] = sum(dat$comp == 1 & dat$treat.gr == i, na.rm = T)/sum(dat$treat.gr == i, na.rm = T)
}

comp.rate <- as.numeric(round(comp.rate, digits = 3)*100)

comp.df <- as.data.frame(cbind(comp.rate, c('1','2','3','4','5','6','7','8')))
colnames(comp.df) <- c('comp.rate', 'treat.gr')
comp.df$comp.rate <- as.numeric(comp.df$comp.rate)

comp.gr.plot <- ggplot(comp.df, aes(x = treat.gr, y = comp.rate)) +
  geom_bar(stat = 'identity') +
  ylim(0, 100) +
  theme(text=element_text(family="serif")) +
  ylab('Compliance Rate (Percentage)') +
  xlab('Treatment Group') + 
 # ggtitle('Compliance Rate Across Treatment Groups, mean compliance = 48.5%') +
  theme(plot.title = element_text(hjust = 0.5)) + theme(text=element_text(family="serif"))

## Compliance Modeling ##

comp.m1 <- lm(comp ~ un.gr + aff.gr.1 + pid + gender + edu + age,
              data = dat)
summary(comp.m1)
nobs(comp.m1)

apsrtable(comp.m1)

```

## Power Analysis

```{r}

library(pwr)



groupmeans <- c(25, 25, 0, 50)

power1 <- power.anova.test(groups = length(groupmeans), 
between.var = var(groupmeans), within.var = 10000, 
power=0.8,sig.level=0.05,n=NULL)
power1

groupmeans2 <- c(40, 65, 40, 65, 50, 50, 25, 75)

power2 <- power.anova.test(groups = length(groupmeans2), 
between.var = var(groupmeans2), within.var = 10000, 
power=0.8,sig.level=0.05,n=NULL)
power2


# medium effect size

power3 <- pwr.2p.test(h = 0.5, sig.level = 0.05, power = 0.80)

plot(power3) # 63 total  (393 for small effect size)

```

## Variance of DV

```{r}

var_pl <- ggplot(dat, aes(x = voting)) +
  geom_density() +
  theme_bw() +
  labs(x = 'Vote Intention', y = 'Density')

var_pl

var_pl1 <- ggplot(dat, aes(x = voting, color = treat.grp.f)) +
  geom_density() +
  theme_bw() +
  labs(x = 'Vote Intention', y = 'Density', color = 'Group')

var_pl1

#ggsave(var_pl, file = "var_pl.pdf", height = 4, width = 6)
#ggsave(var_pl1, file = "var_pl1.pdf", height = 4, width = 6)

```

# Unnecessary Code {.tabset}

```{r}

# m1 <- lm(voting ~ un.aff + un.cog + un.non.aff + un.non.cog +
#                   cert.aff + cert.cog + cert.non.aff +cert.non.cog +
#                   pid +
#                   un.aff*pid + un.cog*pid + un.non.aff*pid + un.non.cog*pid +
#                   cert.aff*pid + cert.cog*pid + cert.non.aff*pid +cert.non.cog*pid,
#                   data = dat)
# summary(m1)
# 
# m2 <- lm(voting ~ un.aff + un.cog + un.non.aff + un.non.cog +
#            cert.aff + cert.cog + cert.non.aff +cert.non.cog +
#            pidstr +
#            un.aff*pidstr + un.cog*pidstr + un.non.aff*pidstr + un.non.cog*pidstr +
#            cert.aff*pidstr + cert.cog*pidstr + cert.non.aff*pidstr +cert.non.cog*pidstr,
#          data = dat)
# summary(m2)
# 
# apsrtable(m1, m2)
# 
# comp.m1 <- lm(voting ~ un.aff + un.cog + un.non.aff + un.non.cog +
#            cert.aff + cert.cog + cert.non.aff +cert.non.cog +
#            pid +
#            un.aff*pid + un.cog*pid + un.non.aff*pid + un.non.cog*pid +
#            cert.aff*pid + cert.cog*pid + cert.non.aff*pid +cert.non.cog*pid,
#          data = subset(dat, comp == 1))
# summary(comp.m1)
# 
# comp.m2 <- lm(voting ~ un.aff + un.cog + un.non.aff + un.non.cog +
#            cert.aff + cert.cog + cert.non.aff +cert.non.cog +
#            pidstr +
#            un.aff*pidstr + un.cog*pidstr + un.non.aff*pidstr + un.non.cog*pidstr +
#            cert.aff*pidstr + cert.cog*pidstr + cert.non.aff*pidstr +cert.non.cog*pidstr,
#          data = subset(dat, comp == 1))
# summary(comp.m2)

```

